<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>行政区划归属获取</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
        body,
        html,
        #container {
            overflow: hidden;
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }

        .info {
            z-index: 999;
            width: auto;
            min-width: 22rem;
            padding: .75rem 1.25rem;
            margin-left: 1.25rem;
            position: fixed;
            top: 1rem;
            background-color: #fff;
            border-radius: .25rem;
            font-size: 14px;
            color: #666;
            box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
        }
    </style>
    <script type="text/javascript" src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=你的密钥"></script>
</head>
<body>
<div id="container"></div>

</body>
</html>
<script>
    var map = new BMapGL.Map('container');
    var point = new BMapGL.Point(116.404, 39.915);
    map.centerAndZoom(point, 15);
    map.enableScrollWheelZoom(true);

    var marker;
    var HostObject = window.chrome.webview.hostObjects.ScriptCallbackObject;
    var gc;
    //自动定位
    var geolocation = new BMapGL.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            marker = new BMapGL.Marker(r.point, {
                enableDragging: true
            });
            map.addOverlay(marker);
            map.panTo(r.point);

            point = new BMapGL.Point(r.point.lng, r.point.lat);
            map.centerAndZoom(point, 15);


            gc = new BMapGL.Geocoder();
            gc.getLocation(point, function (rs) {
                HostObject.ReceivingMessage(rs.addressComponents.province + rs.addressComponents.city + rs.addressComponents.district);
            });
            //alert('您的位置：' + r.point.lng + ',' + r.point.lat);
        }
        else {
            alert('failed' + this.getStatus());
        }
    });

    //点击事件
    map.addEventListener('click', function (e) {
        var point = new BMapGL.Point(e.latlng.lng, e.latlng.lat);

        //清除覆盖物
        map.clearOverlays();
        // 创建点标记
        enableDragging: true
        marker = new BMapGL.Marker(point, {
        });
        map.addOverlay(marker);
        map.panTo(point);

        gc = new BMapGL.Geocoder();
        gc.getLocation(point, function (rs) {
            var opts = {
                title: '行政区划归属',
                width: 220,
                height: 92
            };
            var infoStr = '<div>省：' + rs.addressComponents.province + '</div>'
                + '<div>市：' + rs.addressComponents.city + '</div>'
                + '<div>区：' + rs.addressComponents.district + '</div>';
            var infoWindow = new BMapGL.InfoWindow(infoStr, opts);
            map.openInfoWindow(infoWindow, point);
            HostObject.ReceivingMessage(rs.addressComponents.province + rs.addressComponents.city + rs.addressComponents.district);
        });
    });

    //地名查询定位
    function LocateByName(msg) {
        //清除覆盖物
        map.clearOverlays();

        var local = new BMapGL.LocalSearch(map, {
            renderOptions: { map: map }
        });
        local.search(msg);

        gc = new BMapGL.Geocoder();
        gc.getLocation(point, function (rs) {
            HostObject.ReceivingMessage(rs.addressComponents.province + rs.addressComponents.city + rs.addressComponents.district);
        });
    }
</script>
